
Namespace _2bMatesSite.Controls
	Imports System
	Imports System.Data
	Imports System.Drawing
	Imports System.Web
	Imports System.Web.UI.WebControls
	Imports System.Web.UI.HtmlControls

	''' <summary>
	'''		Summary description for PagingControl.
	''' </summary>
	Public Partial Class PagingControl
		Inherits System.Web.UI.UserControl

		Private currentPageNumber As Integer = 0
		Private firstPageNumber As Integer = 0
		' the first page of page links
		Private lastPageNumber As Integer = 0
		Private maxPageNumber As Integer = 0
		Protected litStartImageIndexDown As System.Web.UI.WebControls.Literal
		Protected litEndImageIndexDown As System.Web.UI.WebControls.Literal
		Protected litAllFriendsCountDown As System.Web.UI.WebControls.Literal
		Private pagesRest As Integer = 0
		Private action As String = "0"
		Private redirectPage As String = ""
		Private validationOn As Boolean = True
		Private beforePageNumberText As String = ""


		Public Property CurrentPageNumber() As Integer
			Get
				Return currentPageNumber
			End Get
			Set
				currentPageNumber = value
			End Set
		End Property

		Public Property FirstPageNumber() As Integer
			Get
				InitFirstPageNumber()
				Return firstPageNumber
			End Get
			Set
				firstPageNumber = value
			End Set
		End Property

		Public Property MaxPageNumber() As Integer
			Get
				Return maxPageNumber
			End Get
			Set
				maxPageNumber = value
			End Set
		End Property

		Public Property PagesRest() As Integer
			Get
				pagesRest = MaxPageNumber - firstPageNumber + 1
				Return pagesRest
			End Get
			Set
				pagesRest = value
			End Set
		End Property

		Public Property LastPageNumber() As Integer
			Get
				lastPageNumber = Math.Min(pagesRest, Constants.MAX_PAGES_NUMS_ON_PAGE)
				Return lastPageNumber
			End Get
			Set
				lastPageNumber = value
			End Set
		End Property

		Public Property Action() As String
			Get
				Return action
			End Get
			Set
				action = value
			End Set
		End Property

		Public Property RedirectPage() As String
			Get
				Return redirectPage
			End Get
			Set
				redirectPage = value
			End Set
		End Property

		Public Property BeforePageNumberText() As String
			Get
				Return beforePageNumberText
			End Get
			Set
				beforePageNumberText = value
			End Set
		End Property

		Public Property ValidationOn() As Boolean
			Get
				Return validationOn
			End Get
			Set
				validationOn = value
			End Set
		End Property

		Public Sub InitFirstPageNumber()
			Dim t As Integer = currentPageNumber
			Dim rem As Integer
			Dim i As Integer = 0
			While i < Constants.MAX_PAGES_NUMS_ON_PAGE
				Math.DivRem(t, Constants.MAX_PAGES_NUMS_ON_PAGE, rem)
				If rem = 1 Then
					firstPageNumber = t
					Exit While
				Else
					System.Math.Max(System.Threading.Interlocked.Decrement(t),t + 1)
				End If
				System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
			End While
		End Sub

		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			RenderPager()
		End Sub

		Public Sub ReloadControl()
			RenderPager()
		End Sub

		Private Sub RenderPager()
			' Put user code to initialize the page here
			litLastNum.Text = MaxPageNumber.ToString()
			lbLastNum.Text = MaxPageNumber.ToString()
			lbLastNum.CommandArgument = MaxPageNumber.ToString()

			' initialize max page number visiblity
			If currentPageNumber = MaxPageNumber Then
				litLastNum.Visible = True
				lbLastNum.Visible = False
			Else
				litLastNum.Visible = False
				lbLastNum.Visible = True
			End If

			' write page links on page and set their command arguments

			Dim i As Integer = 1
			While i <= lastPageNumber
				Dim litTemp As System.Web.UI.WebControls.Literal = DirectCast(Me.FindControl("litNum" + i), System.Web.UI.WebControls.Literal)
				Dim lbTemp As System.Web.UI.WebControls.LinkButton = DirectCast(Me.FindControl("lbNum" + i), System.Web.UI.WebControls.LinkButton)
				litTemp.Text = beforePageNumberText + (i + firstPageNumber - 1).ToString()
				lbTemp.Text = beforePageNumberText + (i + firstPageNumber - 1).ToString()
				lbTemp.CommandArgument = (firstPageNumber + i - 1).ToString()
				If (firstPageNumber + i - 1) = currentPageNumber Then
					litTemp.Visible = True
					lbTemp.Visible = False
				Else
					lbTemp.Visible = True
					litTemp.Visible = False
				End If
				System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
			End While

			If lastPageNumber <= Constants.MAX_PAGES_NUMS_ON_PAGE Then
				Dim i As Integer = lastPageNumber + 1
				While i <= Constants.MAX_PAGES_NUMS_ON_PAGE
					Dim litTemp As System.Web.UI.WebControls.Literal = DirectCast(Me.FindControl("litNum" + i), System.Web.UI.WebControls.Literal)
					Dim lbTemp As System.Web.UI.WebControls.LinkButton = DirectCast(Me.FindControl("lbNum" + i), System.Web.UI.WebControls.LinkButton)
					litTemp.Visible = False
					lbTemp.Visible = False
					System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
				End While
			End If

			lbArrowsNext.CommandArgument = (firstPageNumber + Constants.MAX_PAGES_NUMS_ON_PAGE).ToString()
			lbArrowsPrev.CommandArgument = (firstPageNumber - Constants.MAX_PAGES_NUMS_ON_PAGE).ToString()
			lbNext.CommandArgument = (currentPageNumber + 1).ToString()
			lbPrev.CommandArgument = (currentPageNumber - 1).ToString()

			'set arrows, Prev and Next visibility
			If pagesRest <= Constants.MAX_PAGES_NUMS_ON_PAGE Then
				lbArrowsNext.Visible = False
			Else
				lbArrowsNext.Visible = True
			End If
			If pagesRest = MaxPageNumber Then
				lbArrowsPrev.Visible = False
			Else
				lbArrowsPrev.Visible = True
			End If
			If currentPageNumber = 1 Then
				lbPrev.Visible = False
			Else
				lbPrev.Visible = True
			End If
			If currentPageNumber = MaxPageNumber Then
				lbNext.Visible = False
			Else
				lbNext.Visible = True
			End If

			' set causes validation value
			lbNum1.CausesValidation = validationOn
			lbNum2.CausesValidation = validationOn
			lbNum3.CausesValidation = validationOn
			lbNum4.CausesValidation = validationOn
			lbNum5.CausesValidation = validationOn
			lbLastNum.CausesValidation = validationOn
			lbArrowsNext.CausesValidation = validationOn
			lbLastNum.CausesValidation = validationOn
			lbArrowsPrev.CausesValidation = validationOn
			lbPrev.CausesValidation = validationOn
			lbNext.CausesValidation = validationOn
		End Sub

		Private Sub lbNum_Command(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
			Dim redirectParams As String = ""
			If redirectPage.IndexOf("?") <> -1 Then
				redirectParams = "&page=" + e.CommandArgument
			Else
				redirectParams = "?page=" + e.CommandArgument
			End If
			If action = Constants.SEE_ALL_FRIENDS_EDIT_ACTION Then
				redirectParams += "&action=" + action
			End If

			If Request.UrlReferrer <> Nothing AndAlso Request.UrlReferrer.ToString().IndexOf((Constants.SEARCH)) <> -1 Then
				Response.Redirect(redirectPage + redirectParams + "&#results")
ElseIf Request.UrlReferrer <> Nothing AndAlso Request.UrlReferrer.ToString().IndexOf((Constants.EDIT_GROUP_EVENTS)) <> -1 Then
				Response.Redirect(redirectPage + redirectParams + "&#all_events")
			Else
				Response.Redirect(redirectPage + redirectParams)
			End If
		End Sub

		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		'''		Required method for Designer support - do not modify
		'''		the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()
			AddHandler Me.lbArrowsPrev.Command, AddressOf Me.lbNum_Command
			AddHandler Me.lbNum1.Command, AddressOf Me.lbNum_Command
			AddHandler Me.lbNum2.Command, AddressOf Me.lbNum_Command
			AddHandler Me.lbNum3.Command, AddressOf Me.lbNum_Command
			AddHandler Me.lbNum4.Command, AddressOf Me.lbNum_Command
			AddHandler Me.lbNum5.Command, AddressOf Me.lbNum_Command
			AddHandler Me.lbArrowsNext.Command, AddressOf Me.lbNum_Command
			AddHandler Me.lbLastNum.Command, AddressOf Me.lbNum_Command
			AddHandler Me.lbPrev.Command, AddressOf Me.lbNum_Command
			AddHandler Me.lbNext.Command, AddressOf Me.lbNum_Command

		End Sub
#End Region

		====================
        ' This public property was added by conversion wizard to allow the access of a protected, autogenerated member variable dgListing.
		====================
        Public ReadOnly Property dgListing() As Controls.ListingControl
            Get
                Return Migrated_dgListing
            End Get
        End Property
        'set { Migrated_dgListing = value; }
    End Class
End Namespace






